
#import "Controller.h"

@implementation Controller

@synthesize poly;

- (void) updateView:(int) previousNumSides {
	int num = [poly.shape numberOfSides];
	numberOfSidesLabel.text = [NSString stringWithFormat:@"%d",
					  num];
	if (num <= [poly.shape minimumNumberOfSides]) {
		decreaseButton.enabled = NO;
	}
	else if (num >= [poly.shape maximumNumberOfSides]) {
		increaseButton.enabled = NO;
	}
	else {
		if (!decreaseButton.enabled) {
			decreaseButton.enabled = YES;
		}
		if (!increaseButton.enabled) {
			increaseButton.enabled = YES;
		}
	}
	if (previousNumSides != num) {
		[polyView setNeedsDisplay];
		int savedInt = [poly.shape numberOfSides];
		[[NSUserDefaults standardUserDefaults] setInteger:savedInt forKey:@"beerandjesus.HelloPoly.numberOfSides"];
	}
}

- (IBAction)decrease {
	int previousNumSides = [poly.shape numberOfSides];
    [poly decrease];
	[self updateView:previousNumSides];
}

- (IBAction)increase {
	int previousNumSides = [poly.shape numberOfSides];
	[poly increase];
	[self updateView:previousNumSides];
}

- (void)awakeFromNib {
	int savedInt = [[NSUserDefaults standardUserDefaults] integerForKey:@"beerandjesus.HelloPoly.numberOfSides"];
	if (savedInt < 3) {
		savedInt = numberOfSidesLabel.text.intValue;
	} else {
		numberOfSidesLabel.text = [NSString stringWithFormat:@"%d", savedInt];
	}
	[poly initializeShape:savedInt];
	CGRect frame = CGRectMake (50, 220, 150, 40);
	UILabel *shapeName = [[UILabel alloc] initWithFrame:frame];
	[shapeName setTextAlignment:UITextAlignmentCenter];
	[shapeName setText:[poly.shape name]];
	[polyView addSubview:shapeName];
	[polyView setShapeName:shapeName];
	[shapeName release];
	
}
- (void) dealloc
{
	[decreaseButton release];
	[numberOfSidesLabel release];
	[increaseButton release];
	[polyView release];
	[poly release];
	[super dealloc];
}


@end
